package com.lwj.liwanjia.utils;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.widget.Toast;

import com.lwj.liwanjia.R;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

/**
 * Created by Administrator on 2017/10/30.
 */

public class ShareUtils {
    private static ShareUtils shareUtils;
    private Context context;


    public static ShareUtils getInstance() {
        if (shareUtils == null) {
            synchronized (ShareUtils.class) {
                if (shareUtils == null) {
                    shareUtils = new ShareUtils();
                }
            }
        }
        return shareUtils;
    }

    public void startShare(Context context, int type,String iconUrl,String url,String title,String des) {
        this.context = context;
        UMWeb web = new UMWeb(url);
        web.setTitle(title);//标题
        if(iconUrl!=null&&!TextUtils.isEmpty(iconUrl)){
            UMImage thumb = new UMImage(context,iconUrl);
            web.setThumb(thumb);
        }else{
            UMImage thumb = new UMImage(context,R.mipmap.invit);
            web.setThumb(thumb);
        }
        web.setDescription(des);//描述
        if (type == 0) {
            new ShareAction((Activity) context)
                    .setPlatform(SHARE_MEDIA.QQ)
                    .withMedia(web)
                    .setCallback(shareListener)
                    .share();
        } else if (type == 1) {
            new ShareAction((Activity) context)
                    .setPlatform(SHARE_MEDIA.WEIXIN)
                    .withMedia(web)
                    .setCallback(shareListener)
                    .share();
        } else if (type == 2) {
            new ShareAction((Activity) context)
                    .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)
                    .withMedia(web)
                    .setCallback(shareListener)
                    .share();
        }
    }

    private UMShareListener shareListener = new UMShareListener() {
        /**
         * @descrption 分享开始的回调
         * @param platform 平台类型
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
        }

        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
        }

        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
        }
    };

}
